﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpAlgorithm
{
    class MyChirt
    {
        public MyChirt()
        {
            int testCase = GetConsole.GetInt();

            while (testCase-- > 0)
            {
                Dictionary<string, Chirt> chirtDic = new Dictionary<string, Chirt>();
                List<Member> memberList = new List<Member>();
                //입력
                int shirtCount = GetConsole.GetInt() / 6;
                int memberCount = GetConsole.GetInt();
                for (int i = 0; i < memberCount; i++)
                {
                    string size1 = GetConsole.GetString();
                    if (!chirtDic.ContainsKey(size1))
                    {
                        chirtDic.Add(size1, new Chirt(size1));
                    }
                    chirtDic[size1].AddNumber(i);
                    string size2 = GetConsole.GetString();
                    if (!chirtDic.ContainsKey(size2))
                    {
                        chirtDic.Add(size2, new Chirt(size2));
                    }
                    chirtDic[size2].AddNumber(i);
                    memberList.Add(new Member(size1, size2));
                }
                //처리
                List<Chirt> chirtList = chirtDic.Values.ToList();
                int minNoFixed = int.MinValue;
                int maxValue = int.MaxValue;
                while (minNoFixed < shirtCount && maxValue > shirtCount)
                {
                    //가장 적은 사람이 필요한 셔츠 가져오기
                    minNoFixed = chirtList.Min(c => c.NofixedCount);
                    Chirt minChirt = chirtList.Find(c => c.NofixedCount == minNoFixed);
                    //그 셔츠의 사용자 중에서 가장 많이 필요로 하는 셔츠와 함께 사용가능한 사람으로 결정.
                    int otherChirtCount = 0;
                    int fixMember = -1;
                    foreach (int member in minChirt.NoFixedList)
                    {
                        string otherSize = memberList[member].GetOhterSize(minChirt.Size);
                        if (chirtDic[otherSize].NofixedCount > otherChirtCount)
                        {
                            otherChirtCount = chirtDic[otherSize].NofixedCount;
                            fixMember = member;
                        }
                    }
                    minChirt.FixedNumber(fixMember);
                    maxValue = chirtList.Max(c => c.NofixedCount + c.FixedCount);
                }
                //출력
                if (maxValue <= shirtCount)
                {
                    Console.WriteLine("YES");
                }
                else
                {
                    Console.WriteLine("NO");
                }
            }
        }
    }
    class Chirt
    {
        private string size = string.Empty;
        public string Size { get { return size; } }
        private List<int> fixedList = new List<int>();
        private List<int> nofixedList = new List<int>();
        public List<int> NoFixedList { get { return nofixedList; } }
        public int FixedCount { get { return fixedList.Count; } }
        public int NofixedCount { get { return nofixedList.Count; } }
        public Chirt(string size)
        {
            this.size = size;
        }
        public void FixedNumber(int number)
        {
            nofixedList.Remove(number);
            fixedList.Add(number);
        }
        public void AddNumber(int number)
        {
            nofixedList.Add(number);
        }
    }
    struct Member
    {
        public string size1;
        public string size2;
        public Member(string size1, string size2)
        {
            this.size1 = size1;
            this.size2 = size2;
        }
        public string GetOhterSize(string size)
        {
            if (size == size1)
            {
                return size2;
            }
            else
            {
                return size1;
            }
        }
    }
}
